<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <link rel="stylesheet" href="../../lay/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../lay/style/admin.css" media="all">
  <link rel="stylesheet" href="../../lay/style/yogee.css" media="all">
  <link rel="stylesheet" href="../../lay/lib/extend/dtree/dtree.css" media="all">
  <link rel="stylesheet" href="../../lay/lib/extend/dtree/font/dtreefont.css" media="all">
  <style>

    .layui-card-header .layui-icon {
      position: initial;
      margin-top: 0;
      line-height: unset;
    }

    .layui-upload-img {
      width: 200px;
      /*height: 90px;*/
    }
  </style>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-fluid">
      <div class="layui-tab-content">
        <div class="layui-form" lay-filter="layForm">
          <div class="layui-form-item">
            <label for="name" class="layui-form-label">名字：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="name" name="name" lay-verify="required" placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="password" class="layui-form-label">密码：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="password" type="password" name="password" lay-verify="required"
                     placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="departId" class="layui-form-label">所属部门：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="departId" name="departId" autocomplete="off" type="hidden"
                      placeholder="">
              <input class="layui-input" id="fName" name="fName" autocomplete="off"
                     placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="phone" class="layui-form-label">手机号：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="phone" name="phone" lay-verify="required|phone" placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="entryTime" class="layui-form-label">入职时间：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="entryTime" name="entryTime" lay-verify="required" autocomplete="off"
                     placeholder="">
            </div>
          </div>
          <div class="box-footer layui-text-center">
            <div class="align-center">
              <button lay-submit class="submit layui-btn layui-btn-normal" lay-filter="*"
                      data-loading="<i class='layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop'></i> 提交中">
                提交
              </button>
              <a class="layui-btn layui-btn-warm" href="javascript:history.back(-1)">取消</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="../../util/httpUtil.js"></script>
<script src="../../lay/layui/layui.js"></script>
<script>
  layui.config({
    base: '../../lay'
  }).extend({
    dtree: '/lib/extend/dtree'
  }).use(['form', 'jquery', 'layer', 'element', 'upload', 'laydate', 'dtree'], function () {
    var $ = layui.$;
    var form = layui.form;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var dtree = layui.dtree;
    var imgUrl = '';
    var userId = getUrlParam('id') ? getUrlParam('id') : "";

    laydate.render({
      elem: '#entryTime',
      type: 'datetime',
      range: false
    });

    //数据回显
    if (userId !== "") {
      HTTPUtil.post('/user/sysUser/get', {id: userId})
          .then(function (json) {
            if (json.state === 'success') {
              form.val("layForm", {
                "name": json.data.name,
                "password": json.data.password,
                "postId": json.data.postId,
                "phone": json.data.phone,
                "departId": json.data.departId,
                "entryTime": json.data.entryAt,
              });
              $('#fName').val(json.data.departName);
              getPostList(json.data.postId);
            } else {
              layer.msg(json.msg, {icon: 2});
            }
          });
    } else {
      getPostList("");
    }

    //获取部门树
    $('#fName').click(function () {
      dtree.render({
        elem: "#tree",  //绑定元素
        url: '/yl/ylDepartment/treeList',
        dataFormat: "list",  //异步接口
        initLevel: 2,
        dot: false,
        response: {statusCode: "200", rootName: "data", treeId: "id", parentId: "pid", title: "name"}
      });
      var box = layer.open({
        title: '选择上级部门',
        type: 1,
        content: $('#tree'),
        btn: ['确定', '取消'],
        yes: function (index, layero) {
          layer.close(box);
        }
      })
    });

    //选择对应部门
    dtree.on("node('tree')", function (param) {
      $("#departId").val(param.param.nodeId);
      $("#fName").val(param.param.context);
    });

    form.on('submit(*)', function (data) {
      $(this).addClass('layui-btn-disabled');
      $(this).html($(this).data('loading'));
      var name = $('#name').val();
      var password = $('#password').val();
      var postId = $('#postId').val();
      var departId = $('#departId').val();
      var phone = $('#phone').val();
      var entryTime = $('#entryTime').val();

      HTTPUtil.post('/user/sysUser/store',
          {
            id: userId,
            name: name,
            password: password,
            postId: postId,
            departId: departId,
            phone: phone,
            entryTime: entryTime,
          }).then(function (json) {
        if (json.state === 'success') {
          layer.msg(json.msg, {icon: 1, time: 500}, function () {
            window.location.href = 'userList.html';
          });
        } else {
          layer.msg(json.msg, {icon: 2});
        }
      })

    });

  });

</script>
<ul id="tree" class="dtree" data-id="0" style="display: none"></ul>
</body>
</html>